package bestTeam.WalkieDoggie.client.Composites;

import java.io.IOException;
import bestTeam.WalkieDoggie.client.Gender;
import bestTeam.WalkieDoggie.client.Location;
import bestTeam.WalkieDoggie.client.WalkieDoggie;
import bestTeam.WalkieDoggie.client.Serializables.UserProfileData;
import bestTeam.WalkieDoggie.client.ServicesInterfaces.FacebookUsersService;
import bestTeam.WalkieDoggie.client.ServicesInterfaces.FacebookUsersServiceAsync;
import bestTeam.WalkieDoggie.server.EmailServiceImpl;

import com.google.code.gwteyecandy.Tooltip;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LatLngCallback;
import com.google.gwt.maps.client.geom.LatLng;


public class profilePage extends Composite{
	private Label title;
	private VerticalPanel verPanel;
	private HorizontalPanel horizontalPanel_8;
	private VerticalPanel verticalPanel_1;
	private HorizontalPanel horizontalPanel;
	private Label lblAdress;
	private TextBox countrytextBox;
	private HorizontalPanel horizontalPanel_2;
	private Label lblCity;
	private TextBox citytextBox;
	private HorizontalPanel horizontalPanel_3;
	private Label lblStreet;
	private TextBox streettextBox;
	private HorizontalPanel horizontalPanel_1;
	private Label lblHouseNo;
	private IntegerBox houseintegerBox;

	private IntegerBox phoneintegerBox;
	private VerticalPanel verticalPanel;
	private HorizontalPanel horizontalPanel_5;
	private Label lblDogsName;
	private TextBox dogNametextBox;
	private VerticalPanel hor;
	private Label label;
	private FileUpload fileUpload;
	private HorizontalPanel horizontalPanel_6;
	private Label lblGender;
	private RadioButton rdbtnMale;
	private RadioButton rdbtnFemale;
	private HorizontalPanel horizontalPanel_7;
	private Label lblOkWithOther;
	private CheckBox chckbxMales;
	private CheckBox chckbxFemales;
	private HorizontalPanel horizontalPanel_4;
	private Button btnSave;
	private Button btnCancel;
	private HorizontalPanel hori;
	private Label lblPhoneNo;
	private Label inputStatus;
	private CheckBox wantsDailyEmails;

	public profilePage() {

		verPanel = new VerticalPanel();
		verPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verPanel.setSpacing(5);
		verPanel.setSize("100%", "100%");

		initWidget(verPanel);

		horizontalPanel_8 = new HorizontalPanel();
		horizontalPanel_8.setBorderWidth(0);
		horizontalPanel_8.setSpacing(5);

		title=new Label("Profile Page:");
		title.setStyleName("titleForView");
		verPanel.add(title);
		verPanel.add(horizontalPanel_8);
		verPanel.setCellHorizontalAlignment(title, HasHorizontalAlignment.ALIGN_LEFT);

		verticalPanel_1 = new VerticalPanel();
		verticalPanel_1.setSpacing(5);
		horizontalPanel_8.add(verticalPanel_1);
		verticalPanel_1.setSize("100%", "100%");
		verticalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);

		horizontalPanel_2 = new HorizontalPanel();
		horizontalPanel_2.setSpacing(5);
		verticalPanel_1.add(horizontalPanel_2);
		horizontalPanel_2.setWidth("100%");
		horizontalPanel_2.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		lblCity = new Label("City:");
		horizontalPanel_2.add(lblCity);

		citytextBox = new TextBox();
		horizontalPanel_2.add(citytextBox);
		horizontalPanel_2.setCellHorizontalAlignment(citytextBox, HasHorizontalAlignment.ALIGN_RIGHT);
		citytextBox.setWidth("161px");

		Tooltip t = new Tooltip();
		t.setText("Address is needed for geo-based filtering");
		t.attachTo(citytextBox);

		horizontalPanel_3 = new HorizontalPanel();
		horizontalPanel_3.setSpacing(5);
		verticalPanel_1.add(horizontalPanel_3);
		horizontalPanel_3.setWidth("100%");
		horizontalPanel_3.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		lblStreet = new Label("Street: ");
		horizontalPanel_3.add(lblStreet);

		streettextBox = new TextBox();
		horizontalPanel_3.add(streettextBox);
		horizontalPanel_3.setCellHorizontalAlignment(streettextBox, HasHorizontalAlignment.ALIGN_RIGHT);
		streettextBox.setWidth("159px");

		horizontalPanel_1 = new HorizontalPanel();
		horizontalPanel_1.setSpacing(5);
		verticalPanel_1.add(horizontalPanel_1);
		horizontalPanel_1.setWidth("100%");
		horizontalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		lblHouseNo = new Label("House number:");
		horizontalPanel_1.add(lblHouseNo);
		lblHouseNo.setWidth("100%");

		houseintegerBox = new IntegerBox();
		horizontalPanel_1.add(houseintegerBox);
		horizontalPanel_1.setCellHorizontalAlignment(houseintegerBox, HasHorizontalAlignment.ALIGN_RIGHT);
		houseintegerBox.setWidth("70px");


		Tooltip t2 = new Tooltip();
		t2.setText("Exact address isn't visible to everybody");
		t2.attachTo(houseintegerBox);

		houseintegerBox.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(!Character.isDigit(event.getCharCode()))
					((IntegerBox)event.getSource()).cancelKey();
			}
		});

		hori = new HorizontalPanel();
		hori.setSpacing(5);
		verticalPanel_1.add(hori);
		hori.setWidth("100%");
		hori.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		lblPhoneNo = new Label("Phone:");
		hori.add(lblPhoneNo);
		lblHouseNo.setWidth("100%");

		phoneintegerBox = new IntegerBox();
		hori.add(phoneintegerBox);
		hori.setCellHorizontalAlignment(phoneintegerBox, HasHorizontalAlignment.ALIGN_RIGHT);
		phoneintegerBox.setWidth("150px");

		phoneintegerBox.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(!Character.isDigit(event.getCharCode()))
					((IntegerBox)event.getSource()).cancelKey();
			}
		});

		verticalPanel = new VerticalPanel();
		verticalPanel.setSpacing(5);
		verticalPanel.setBorderWidth(0);
		verticalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);

		horizontalPanel_8.add(verticalPanel);
		verticalPanel.setHeight("100%");

		horizontalPanel_5 = new HorizontalPanel();
		horizontalPanel_5.setSpacing(5);
		verticalPanel.add(horizontalPanel_5);
		horizontalPanel_5.setWidth("100%");
		horizontalPanel_5.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		lblDogsName = new Label("Dog's Name: ");
		horizontalPanel_5.add(lblDogsName);

		dogNametextBox = new TextBox();
		horizontalPanel_5.add(dogNametextBox);
		horizontalPanel_5.setCellVerticalAlignment(dogNametextBox, HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel_5.setCellHorizontalAlignment(dogNametextBox, HasHorizontalAlignment.ALIGN_RIGHT);
		dogNametextBox.setWidth("80%");

		hor = new VerticalPanel();
		hor.setSpacing(5);
		hor.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel.add(hor);
		hor.setWidth("100%");

		label = new Label("Dog's Photo: ");
		//hor.add(label);

		fileUpload = new FileUpload();
		//hor.add(fileUpload);
		hor.setCellVerticalAlignment(fileUpload, HasVerticalAlignment.ALIGN_MIDDLE);
		hor.setCellHorizontalAlignment(fileUpload, HasHorizontalAlignment.ALIGN_RIGHT);

		horizontalPanel_6 = new HorizontalPanel();
		horizontalPanel_6.setSpacing(5);
		horizontalPanel_6.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel.add(horizontalPanel_6);
		verticalPanel.setCellHorizontalAlignment(horizontalPanel_6, HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel_6.setWidth("100%");

		lblGender = new Label("Dog's Gender:");
		horizontalPanel_6.add(lblGender);

		rdbtnMale = new RadioButton("new name", "Male");
		rdbtnMale.setChecked(true);
		horizontalPanel_6.add(rdbtnMale);

		rdbtnFemale = new RadioButton("new name", "Female");
		horizontalPanel_6.add(rdbtnFemale);

		horizontalPanel_7 = new HorizontalPanel();
		horizontalPanel_7.setSpacing(5);
		horizontalPanel_7.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel.add(horizontalPanel_7);
		horizontalPanel_7.setWidth("100%");

		lblOkWithOther = new Label("OK with other ");
		horizontalPanel_7.add(lblOkWithOther);

		chckbxMales = new CheckBox("male dogs");
		chckbxMales.setChecked(true);
		horizontalPanel_7.add(chckbxMales);

		chckbxFemales = new CheckBox("female dogs");
		chckbxFemales.setChecked(true);
		horizontalPanel_7.add(chckbxFemales);

		inputStatus = new Label();

		verPanel.add(inputStatus);


		btnSave = new Button("Save Changes");
		btnSave.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Boolean b = verifyInput();

				if(b){
					inputStatus.setText("");

					WalkieDoggie.loginInfo.getUserData().setCity(citytextBox.getText());
					WalkieDoggie.loginInfo.getUserData().setStreet(streettextBox.getText());
					WalkieDoggie.loginInfo.getUserData().setHouseNumber(houseintegerBox.getText());
					WalkieDoggie.loginInfo.getUserData().setPhone(phoneintegerBox.getText());
					WalkieDoggie.loginInfo.getUserData().setDogName(dogNametextBox.getText());
					if(rdbtnMale.isChecked()) 
						WalkieDoggie.loginInfo.getUserData().setDogGender(Gender.Male);
					else WalkieDoggie.loginInfo.getUserData().setDogGender(Gender.Female);
					WalkieDoggie.loginInfo.getUserData().setOKwithFemales(chckbxFemales.getValue());
					WalkieDoggie.loginInfo.getUserData().setOKwithMales(chckbxMales.getValue());
					WalkieDoggie.loginInfo.getUserData().setWantDailyEmails(wantsDailyEmails.getValue());

	
					try {
						getGeoLocationAndAddAppUser();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						WalkieDoggie.handleError(e);
					}

				}
				else{
					inputStatus.setText("***check your input***");
				}
			}
		});


		HorizontalPanel horiz = new HorizontalPanel();
		horiz.setSpacing(5);
		horiz.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horiz.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		horiz.setWidth("100%");
		verticalPanel.add(horiz);
		horiz.setWidth("100%");
		verPanel.setCellHorizontalAlignment(horiz, HasHorizontalAlignment.ALIGN_CENTER);

		wantsDailyEmails = new CheckBox();
		wantsDailyEmails.setChecked(true);
		horiz.add(wantsDailyEmails);
		Label l = new Label("I want daily reminders via Email");
		horiz.add(l);

		horiz.setCellHorizontalAlignment(l, HasHorizontalAlignment.ALIGN_LEFT);
		horiz.setCellHorizontalAlignment(wantsDailyEmails, HasHorizontalAlignment.ALIGN_RIGHT);
		btnSave.setWidth("100px");
		verPanel.add(btnSave);

		Button bb = new Button("for Debug: send me email now");

		bb.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				WalkieDoggie.usersService.sendDailyEmail(WalkieDoggie.loginInfo.getUserData(), new AsyncCallback<Void>() {

					@Override
					public void onSuccess(Void result) {	
					}

					@Override
					public void onFailure(Throwable caught) {	
					}
				});	
			}
		});
		//horiz.add(bb);

		fillFields();
	}

	private void getGeoLocationAndAddAppUser() throws IOException {
		String city = WalkieDoggie.loginInfo.getUserData().getCity();
		String street = WalkieDoggie.loginInfo.getUserData().getStreet();
		String houseNumber = WalkieDoggie.loginInfo.getUserData().getHouseNumber();
		String fullAddress = houseNumber+" "+street+", "+city+", israel";

		Geocoder g = new Geocoder();
		g.getLatLng(fullAddress, new LatLngCallback() {

			@Override
			public void onSuccess(LatLng point) {

				if(point.getLatitude()==0 || point.getLongitude()==0){							String errorMessage = "Sorry, but we could'nt find your address.\nPlease verify that your address is corect.";						WalkieDoggie.handleError(new Exception(errorMessage));						return;
				}	
				final Location location =  new Location(point.getLatitude(), point.getLongitude());
				WalkieDoggie.loginInfo.getUserData().setGeoLocation(location);
				WalkieDoggie.usersService.addAppUser(WalkieDoggie.loginInfo.getUserData(),WalkieDoggie.loginInfo.getAccessToken(), new AsyncCallback<Void>() {

					@Override
					public void onSuccess(Void result) {
						System.out.println("recieved info");
						WalkieDoggie.loginInfo.setIsnewUser(false);

						WalkieDoggie.reloadComposites();
						WalkieDoggie.home_comp.getTabPanel().selectTab(0);
					}

					@Override
					public void onFailure(Throwable caught) {
						WalkieDoggie.handleError(caught);
					}
				});

		}

			@Override
			public void onFailure() {

				String errorMessage = "Sorry, but we could'nt find your address.\n Please verify that your address is corect.";
				WalkieDoggie.handleError(new Exception(errorMessage));
			}
		});


	}

	@SuppressWarnings("deprecation")
	protected Boolean verifyInput() {

		//return true;
		return 	!citytextBox.getText().isEmpty()&&
				!streettextBox.getText().isEmpty()&&
				!houseintegerBox.getText().isEmpty()&&
				!phoneintegerBox.getText().isEmpty()&&				
				//!dogNametextBox.getText().isEmpty()&&				
				(chckbxMales.isChecked()||chckbxFemales.isChecked());

	}

	private Boolean disgardInput() {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("deprecation")
	public void fillFields(){

		UserProfileData currUserData = WalkieDoggie.loginInfo.getUserData();
		if(currUserData.getCity()!=null)
			citytextBox.setText(currUserData.getCity());
		if(currUserData.getStreet()!=null)
			streettextBox.setText(currUserData.getStreet());
		if(currUserData.getHouseNumber()!=null)
			houseintegerBox.setText(currUserData.getHouseNumber());
		if(currUserData.getPhone()!=null)
			phoneintegerBox.setText(currUserData.getPhone());
		if(currUserData.getDogName()!=null)
			dogNametextBox.setText(currUserData.getDogName());
		if (currUserData.getDogGender()!=null){
			rdbtnFemale.setChecked(currUserData.getDogGender().equals(Gender.Female));
			rdbtnMale.setChecked(currUserData.getDogGender().equals(Gender.Male));
		}
		if(currUserData.getCity()!=null){
			chckbxFemales.setChecked(currUserData.getOKwithFemales());
			chckbxMales.setChecked(currUserData.getOKwithMales());
		}
		if(currUserData.getWantDailyEmails()!=null)
			wantsDailyEmails.setChecked(currUserData.getWantDailyEmails());
	}


}
